/**
 * @Title: AttentionDynamicsActivity.java
 * @Copyright: XXX Co., Ltd. Copyright YYYY-YYYY,  All rights reserved
 * @Description: TODO<请描述此文件是做什么的>
 * @author: MeiYi
 * @data: 2015-12-7 上午10:20:14
 * @version: V1.0
 */

package artid.com.arttogo.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.android.volley.VolleyError;

import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.activity.CollectionDetailPagerActivity;
import artid.com.arttogo.adapter.AttentionDyncAdapter;
import artid.com.arttogo.bean.Follow;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.NetUtils;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;
import artid.com.arttogo.view.pulllist.PullableListView;

import static com.taobao.accs.ACCSManager.mContext;

/**
 * 关注动态
 *
 * @author 熊彬
 * @data: 2015-12-7 上午10:20:14
 * @version: V1.0
 */
public class AttentionDynamicsActivity extends BaseFragment {
    private View mView;
    PullToRefreshLayout mPullLayout;
    PullableListView mlistview;
    int pagesize = 20, pageIndex = 1;
    AttentionDyncAdapter adapter;
    View mViewBlank;
    private String imgwide;// 请求的图片宽度

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        mView = inflater.inflate(R.layout.attention_dynamic_layout, null);
        mContext=getActivity();
        mViewBlank = mView.findViewById(R.id.dyn_blank);
        imgwide = (Integer.parseInt(NetUtils.getDisplayMetrics((Activity) mContext).get(NetUtils.DM_WIDTH))) + "";
//		mBack = (ImageView) mView.findViewById(R.id.back_btn);
//		mTittle = (TextView) mView.findViewById(R.id.tittle_text);
//		mBack.setVisibility(View.GONE);
//		mTittle.setText("关注动态");
        mPullLayout = (PullToRefreshLayout) mView.findViewById(R.id.refresh_view);
        mlistview = (PullableListView) mView.findViewById(R.id.listivew);
        mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                // TODO Auto-generated method stub
                pageIndex = 1;
                reqData();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                // TODO Auto-generated method stub
                // pullToRefreshLayout.setBottomDismiss();
                pageIndex++;
                reqData();
            }
        });

        return mView;
    }


    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        reqData();
    }

    void setitemClick(final List<Follow> list) {
        mlistview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(getActivity(), CollectionDetailPagerActivity.class);
                intent.putExtra("id", list.get(arg2).getArtid());
                startActivity(intent);
            }
        });
    }

    void reqData() {
        VolleyBasic.getInstance(getActivity()).post(Cache.follow,
                VolleyMap.getInstance().follow(String.valueOf(pageIndex), String.valueOf(pagesize),
                        AppUtil.getUser().getToken(),imgwide),
                Follow.class,
                new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);

                        if (vd.getCode() == -99) {
                            AppUtil.Logout(getActivity());
                            AppUtil.LoginVer(getActivity());
                            return;
                        }
                        final List<Follow> list = (List<Follow>) vd
                                .getList();
                        if (pageIndex == 1 && list.isEmpty()) {
                            mViewBlank.setVisibility(View.VISIBLE);
                            return;
                        }
                        mViewBlank.setVisibility(View.GONE);
                        if (pageIndex != 1) {
                            adapter.addData(list);
                            setitemClick(adapter.mList);
                            return;
                        }
                        adapter = new AttentionDyncAdapter((Context) getActivity(), list);
                        mlistview.setAdapter(adapter);
                        setitemClick(adapter.mList);

                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub
                        mPullLayout.loadmoreFinish(mPullLayout.FAIL);
                    }
                });
    }
}
